var express = require('express');
var router = express.Router();
const db = require("../common/mysql");
const request = require("request");
const xml2js = require('xml2js');

/* GET home page. */
router.get('/', async function (req, res, next) {
  await db.query('select * from user', function (err, rows) {
    if (!err) {
      data = rows;
    } else {
      data = [];
    }
    console.log('data', data)
    res.render('index', { title: '这是一个简单的demo', data });
  })
  console.log('ss')
});

router.get('/test', function (req, res) {
  var options = {
    headers: { "Connection": "close" },
    url: 'https://secure.shippingapis.com/ShippingAPI.dll?API=TrackV2&XML=<TrackRequest USERID="597CHINA3162"><TrackID ID="9300169903503193306666"></TrackID><TrackID ID="9300169903503195397754"></TrackID></TrackRequest>',
    method: 'GET',
  };

  function callback(error, response, data) {
    if (!error && response.statusCode == 200) {
      console.log('------接口数据------', data);
      xml2js.parseString(data, function (err, result) {
        console.log(result);
        res.send({ code: 200, obj: result, msg: 'success' })
        // {root: "Hello xml2js!"}
      });
    }
  }

  request(options, callback)
})

module.exports = router;
